package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.User.ShipAddressItem;

/**
 * Created by xiong on 2015/12/16.
 */
public class AddressAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ShipAddressItem> datas;

    public AddressAdapter(Context context, ArrayList<ShipAddressItem> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ShipAddressItem shipAddressItem = datas.get(position);

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_ship_address_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.shipAddressName.setText(shipAddressItem.shouhuoren);
        viewHolder.shipAddressMobile.setText(shipAddressItem.shouji);
        SpannableStringBuilder builder = null;
        if (shipAddressItem.moren.equals("Y")) {
            builder = new SpannableStringBuilder("[默认]");
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.blue)), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            builder = new SpannableStringBuilder("");
        }
        String dizhi = shipAddressItem.sheng +
                shipAddressItem.shi + shipAddressItem.xian + " " + shipAddressItem.jiedao;
        builder.append(dizhi);
        viewHolder.shipAddressDetail.setText(builder);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_ship_address_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.ship_address_name)
        TextView shipAddressName;
        @Bind(R.id.ship_address_identity)
        TextView shipAddressIdentity;
        @Bind(R.id.ship_address_mobile)
        TextView shipAddressMobile;
        @Bind(R.id.name_add_mobile)
        RelativeLayout nameAddMobile;
        @Bind(R.id.ship_address_detail)
        TextView shipAddressDetail;
        @Bind(R.id.item_wrapper)
        LinearLayout itemWrapper;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
